<template>
  <div>
    <h2>使用 element-ui</h2>
    <h3>1.0 - 按钮</h3>
    <el-row>
      <el-button>默认按钮</el-button>
      <el-button type="primary" plain>主要按钮</el-button>
      <el-button type="success" round>成功按钮</el-button>
      <el-button type="info">信息按钮</el-button>
      <el-button type="warning">警告按钮</el-button>
      <el-button type="danger">危险按钮</el-button>
    </el-row>
    <h3>2.0 - 输入框</h3>
    <el-row>
      <el-input
        prefix-icon="el-icon-user-solid"
        v-model="val"
        placeholder="请输入内容"
      ></el-input>
      <el-input
        prefix-icon="el-icon-lock"
        show-password
        v-model="val"
        placeholder="请输入密码"
      ></el-input>
    </el-row>
    <h3>3.0 - 表单</h3>
    <div>
      <h4>3.1 - 基本使用</h4>
      <!-- el-form 表单 -->
      <!-- model：表单指定的数据源 label-width：提示文本的宽度 -->
      <el-form ref="form" :model="form" label-width="80px">
        <!-- el-form-item 表单域 -->
        <!-- label：提示文本的内容 -->
        <el-form-item label="活动名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">立即创建</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
      <h4>3.2 - 表单验证</h4>
      <el-form :rules="rules" :model="form" label-width="80px">
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="form.pwd" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button>提交</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
      <h4>3.3 - 自定义验证规则</h4>
      <el-form :rules="rules" :model="form" label-width="80px">
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button>提交</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
      <h4>3.4 - 全局表单验证</h4>
      <el-form ref="form4" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="form.pwd"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="submit4">提交</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
      <h4>3.5 - 局部表单验证</h4>
      <el-form ref="form5" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="验证码">
          <el-input v-model="form.code"></el-input>
          <el-button @click="getCode">获取验证码</el-button>
        </el-form-item>
        <el-form-item>
          <el-button>提交</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <h3>4.0 - 加载</h3>
    <h4>4.1 - 按钮中的加载</h4>
    <el-button :loading="false">登录</el-button>
    <h4>4.2 - 放在元素中的 loading</h4>
    <el-button @click="loading = true" :loading="loading"
      >点我加载数据</el-button
    >
    <el-button @click="loading = false">点我结束加载</el-button>
    <div class="box" v-loading="loading"></div>
    <h3>5.0 - 菜单栏</h3>
    <!-- el-menu：菜单栏的容器 -->
    <el-menu default-active="2" class="el-menu-vertical-demo">
      <!-- el-submenu：菜单栏选项(有二级菜单) -->
      <!-- el-menu-item：菜单栏选项（无二级菜单） -->
      <el-menu-item index="2">
        <i class="el-icon-menu"></i>
        <span slot="title">导航二</span>
      </el-menu-item>
      <el-menu-item index="3">
        <i class="el-icon-document"></i>
        <span slot="title">导航三</span>
      </el-menu-item>
      <el-menu-item index="4">
        <i class="el-icon-setting"></i>
        <span slot="title">导航四</span>
      </el-menu-item>
    </el-menu>
  </div>
</template>

<script>
export default {
  data () {
    return {
      val: '',
      // 表单的数据源
      form: {
        name: '',
        pwd: '',
        mobile: '',
        code: ''
      },
      // 表单的验证规则
      rules: {
        name: [
          // 必填
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // 长度 3~5
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        pwd: [
          // 必填
          { required: true, message: '请输入密码', trigger: 'blur' },
          // 长度 3~5
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        mobile: [
          // 必填
          { required: true, message: '请输入手机号', trigger: 'blur' },
          // 匹配手机的正则
          {
            validator: function (rule, value, callback) {
              // rule：校验的规则（在自定义中，一般用不上）
              // value：要校验的数据
              // callback：回调函数
              //  如果校验通过，执行：callback()
              //  如果校验不通过，执行： callback(new Error('参数验证失败'))
              // 完成手机正则的匹配
              // 定义一个手机的正则
              var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              // 测试 value 是否满足 test 匹配正则
              var bool = reg.test(value)
              bool ? callback() : callback(new Error('请输入合法的手机号'))
            },
            trigger: 'blur'
          }
        ]
      },
      // 加载效果
      loading: false
    }
  },
  methods: {
    getCode () {
      // 验证手机号是否合法
      this.$refs.form5.validateField('mobile', valid => {
        // valid：
        //  为空：说明验证通过
        //  否则：说明验证不通过
        if (!valid) {
          console.log('验证通过')
        } else {
          console.log(valid)
        }
      })
    },
    submit4 () {
      // 得到 form 对象 调用它的全局校验方法
      this.$refs.form4.validate(valid => {
        // valid：校验通过 true 否则为 false
        if (valid) {
          console.log('验证通过，执行后续代码')
        } else {
          console.log('验证失败，重新输入')
        }
      })
    }
  }
}
</script>

<style>
.box {
  width: 800px;
  height: 400px;
  background-color: #ddd;
  border: 5px solid #999;
  margin: 0 auto;
}
</style>
